﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

using System.Windows.Forms;

// add usings
using System.Data.OleDb;

namespace CIS470MetricsTracking
{
    public class UserController
    {
		User user;

		public UserController()
		{
			user = new User();
		}

        public string getUserEmail()
        {
			return user.getEmail();
        }

        public int getUserAccessLevel()
        {
			return user.getAccessLevel();
        }

        public bool verify_user(string email, string password)
        {
			string query = "select firstName, lastName, email, companyPhone, positionId, password from dboUser where email='" + email +"'";
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();

			ds = ControllerManager.data_controller.executeSelectQuery(query);

			if (ds != null) {
				if (ds.Tables.Count != 0) {
					dt = ds.Tables[0];
				} else {
					return false;
				}
				if (dt.Rows.Count != 0) {
					if (dt.Rows[0].ItemArray[5].ToString().Equals(password)) {
						user.setFirstName(dt.Rows[0].ItemArray[0].ToString());
						user.setLastName(dt.Rows[0].ItemArray[1].ToString());
						user.setEmail(dt.Rows[0].ItemArray[2].ToString());
						user.setCompanyPhone(dt.Rows[0].ItemArray[3].ToString());
						user.setAccessLevel((int)dt.Rows[0].ItemArray[4]);
						return true;
					} else {
						return false;
					}
				} else {
					return false;
				}
			} else {
				return false;
			}
        }

        public bool create_user(User new_user)
        {
            string query = "";

			query = "insert into dboUser (firstName, lastName, email, companyPhone, regDate, positionId, [password], accessLevel) VALUES ('" +
                new_user.getFirstName() + "', '" + new_user.getLastName() + "', '" + new_user.getEmail() + "', '" +
				new_user.getCompanyPhone() + "', '" + new_user.getRegDate() +
				"', " + new_user.getAccessLevel() + ", '" + new_user.getPassword() + "', " + new_user.getAccessLevel() + ")";

            //MessageBox.Show(query);

            if (!ControllerManager.data_controller.executeQuery(query))
            {
                return false;
            }

            return true;
        }

		public bool update_user(User user, int userId)
		{
			string query = "";
			query = "update dboUser set firstName='" + user.getFirstName() + "', " + "lastName='" + user.getLastName() + "', " +
				"email='" + user.getEmail() + "', " + "companyPhone='" + user.getCompanyPhone() + "', " +
				"positionId=" + user.getAccessLevel() + ", " + "[password]='" + user.getPassword() + "', " + "accessLevel=" +
				user.getAccessLevel() + " where userId=" + userId;

			if (!ControllerManager.data_controller.executeQuery(query)) {
				return false;
			}

			return true;
		}

		internal void log_out()
		{
			user = new User();
		}

        public bool check_email(string email)
        {
            string query = "select email from dboUser where email='" + email + "'";

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            ds = ControllerManager.data_controller.executeSelectQuery(query);

            if (ds != null)
            {
                if (ds.Tables.Count != 0)
                {
                    dt = ds.Tables[0];
                }
                else
                {
                    return false;
                }
                
                if (dt.Rows.Count != 0)
                {
                    if (dt.Rows[0].ItemArray[0].ToString().Equals(email))
                    {
                        return true;
                    }else
                    {
                        return false;
                    }
                }else
                {
                    return false;
                }
            }else
            {
                return false;
            }
        }

        public bool check_phone(string phone)
        {
            string query = "select phone from dboUser where companyPhone='" + phone + "'";

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            ds = ControllerManager.data_controller.executeSelectQuery(query);

            if (ds != null)
            {
                if (ds.Tables.Count != 0)
                {
                    dt = ds.Tables[0];
                }
                else
                {
                    return false;
                }

                if (dt.Rows.Count != 0)
                {
                    if (dt.Rows[0].ItemArray[0].ToString().Equals(phone))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
	}
}
